
package com.wyzz.hr.bo.impl;

import java.util.List;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.wyzz.hr.bo.IStatBO;
import com.wyzz.hr.dao.IStatusDAO;
import com.wyzz.hr.pojo.Statusconf;
import com.wyzz.hr.pojo.StatusconfPK;


public class StatBOImpl implements IStatBO {

	public StatBOImpl() {
		statusDAO = null;
	}

	public List FindAllStatus() {
		DetachedCriteria dc = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Statusconf.class);
		return statusDAO.findByCriteria(dc);
	}

	public boolean addStat(Statusconf stat) {
		try {
			String trimmedStatusconfTablename = stat.getId()
					.getStatusconfTablename().trim();
			List old = statusDAO.exeHqlList((new StringBuilder()).append(
					" from Statusconf where id.statusconfNo =").append(
					stat.getId().getStatusconfNo()).append(
					" and id.statusconfTablename ='").append(
					trimmedStatusconfTablename).append("'").toString());
			if (old == null || old.size() == 0) {
				stat.getId().setStatusconfTablename(trimmedStatusconfTablename);
				statusDAO.saveObject(stat);
				return true;
			}
		} catch (Exception e) {
			return false;
		}
		return false;
	}

	public boolean delStat(Class name, StatusconfPK id) {
		try {
			Statusconf oldstat = (Statusconf) statusDAO.loadObject(name, id,
					null, new boolean[0]);
			if (oldstat != null) {
				statusDAO.deleteObject(oldstat);
				return true;
			}
		} catch (Exception e) {
			return false;
		}
		return false;
	}

	public boolean updateStatus(Statusconf stat) {
		Statusconf oldstat = (Statusconf) statusDAO.loadObject(
				com.wyzz.hr.pojo.Statusconf.class, stat.getId(), null,
				new boolean[0]);
		if (oldstat != null) {
			String trimmedStatusconfTablename = stat.getId()
					.getStatusconfTablename().trim();
			stat.getId().setStatusconfTablename(trimmedStatusconfTablename);
			oldstat.setId(stat.getId());
			oldstat.setStatusconfDesc(stat.getStatusconfDesc());
			statusDAO.updateObject(oldstat);
			return true;
		} else {
			return false;
		}
	}

	public Statusconf getStatusByName(String tableName, String statusName) {
		return statusDAO.getStatusByName(tableName, statusName);
	}

	public List getAllTableStatus(String tableName) {
		return statusDAO.getStatusByTable(tableName);
	}

	public List getStatusByConfigsAndTable(String tableName,
			List statusConfigNos) {
		DetachedCriteria detachedCriteria = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Statusconf.class);
		detachedCriteria.add(Restrictions.in((new StringBuilder()).append(
				Statusconf.PROP_ID).append(".statusconfNo").toString(),
				statusConfigNos));
		detachedCriteria.add(Restrictions.eq((new StringBuilder()).append(
				Statusconf.PROP_ID).append(".statusconfTablename").toString(),
				tableName));
		List result = statusDAO.findByCriteria(detachedCriteria);
		if (result.size() > 0)
			return result;
		else
			return null;
	}

	public Statusconf getStatusByConfigAndTable(String tableName,
			int statusConfigNo) {
		DetachedCriteria detachedCriteria = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Statusconf.class);
		detachedCriteria.add(Restrictions.eq(Statusconf.PROP_ID,
				new StatusconfPK(new Integer(statusConfigNo), tableName)));
		List result = statusDAO.findByCriteria(detachedCriteria);
		if (result.size() > 0)
			return (Statusconf) result.get(0);
		else
			return null;
	}

	public IStatusDAO getStatusDAO() {
		return statusDAO;
	}

	public void setStatusDAO(IStatusDAO statusDAO) {
		this.statusDAO = statusDAO;
	}

	private IStatusDAO statusDAO;
}
